﻿using System;
using System.Web.Mvc;
using Tyshka.Subroutines.Kernel;

namespace Tyshka.Subroutines.Infrastructure
{
    public static class CustomUrlHelpers
    {
        #region [Subroutine Index]

        public static String SubroutineIndexAction(this UrlHelper helper)
        {
            return helper.Action(Constants.Mvc.Action.Index, Constants.Mvc.Controller.Subroutine);
        }

        public static String IndexAction(this UrlHelper helper)
        {
            return helper.Action(Constants.Mvc.Action.Index, Constants.Mvc.Controller.Home);
        }

        #endregion

        #region [Subroutine Detail]

        public static String SubroutineDetailAction(this UrlHelper helper, long id)
        {
            return helper.Action(Constants.Mvc.Action.Detail, Constants.Mvc.Controller.Subroutine, new {id});
        }

        public static MvcHtmlString SubroutineDetailLink(this UrlHelper helper, long id, String text)
        {
            return Anchor(helper, id, helper.SubroutineDetailAction(id), text, String.Empty);
        }

        #endregion

        #region [Subroutine Edit]

        public static String SubroutineEditAction(this UrlHelper helper, long id)
        {
            return helper.Action(Constants.Mvc.Action.Edit, Constants.Mvc.Controller.Subroutine, new {id});
        }

        public static MvcHtmlString SubroutineEditLink(this UrlHelper helper, long id, String text)
        {
            return helper.SubroutineEditLink(id, text, String.Empty);
        }

        public static MvcHtmlString SubroutineEditLink(this UrlHelper helper, long id, String text, String cssClass)
        {
            return Anchor(helper, id, helper.SubroutineEditAction(id), text, cssClass);
        }

        #endregion

        #region [Subroutine Delete]

        public static String SubroutineDeleteAction(this UrlHelper helper, long id)
        {
            return helper.Action(Constants.Mvc.Action.Delete, Constants.Mvc.Controller.Subroutine, new {id});
        }

        public static MvcHtmlString SubroutineDeleteLink(this UrlHelper helper, long id, String text)
        {
            return helper.SubroutineDeleteLink(id, text, String.Empty);
        }

        public static MvcHtmlString SubroutineDeleteLink(this UrlHelper helper, long id, String text, String cssClass)
        {
            return Anchor(helper, id, helper.SubroutineDeleteAction(id), text, cssClass);
        }

        #endregion

        #region [Tag Detail]

        public static String TagDetailAction(this UrlHelper helper, long id)
        {
            return helper.Action(Constants.Mvc.Action.Detail, Constants.Mvc.Controller.Tag, new {id});
        }

        public static MvcHtmlString TagDetailLink(this UrlHelper helper, long id, String text)
        {
            return helper.TagDetailLink(id, text, String.Empty);
        }

        public static MvcHtmlString TagDetailLink(this UrlHelper helper, long id, String text, String cssClass)
        {
            return Anchor(helper, id, helper.TagDetailAction(id), text, cssClass);
        }

        #endregion

        public static MvcHtmlString Anchor(this UrlHelper helper, long id, String href, String text, String cssClass)
        {
            var anchor = new TagBuilder("a");
            anchor.MergeAttribute("href", href);

            if (!String.IsNullOrEmpty(cssClass))
            {
                anchor.AddCssClass(cssClass);
            }

            anchor.SetInnerText(text);
            return new MvcHtmlString(anchor.ToString());
        }
    }
}